package com.myjob.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myjob.config.Constants;
import com.myjob.dto.ProfileDto;
import com.myjob.entity.*;
import com.myjob.mapper.HuntingStatusMapper;
import com.myjob.response.Result;
import com.myjob.service.*;
import com.myjob.utils.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Chen Yuan
 * @create 2021-01-27  17:08
 */
@Service
@Slf4j
public class HuntingStatusServiceImpl extends ServiceImpl<HuntingStatusMapper, HuntingStatus> implements HuntingStatusService {
	@Autowired
	private HuntingStatusMapper huntingStatusMapper;

	@Autowired
	private ProfileService profileService;
	@Autowired
	private BaseInfoService baseInfoService;

	/**
	 * 分页查询
	 * @param currentPage 当前页
	 * @param pageSize 页大小
	 * @param userName 用户姓名
	 * @param hrName hr 姓名
	 * @return
	 */
	@Override
	public Result selectHuntingStatus(Integer currentPage, Integer pageSize, String userName, String hrName, String userId, String searchValue) {
		Result result = null;
		QueryWrapper<HuntingStatus> wrapper = new QueryWrapper<>();
		Page page = null;

		if (UtilValidate.isNotEmpty(userName)){
			wrapper.like("user_name", userName);
		}
		if (UtilValidate.isNotEmpty(hrName)){
			wrapper.like("hr_name", hrName);
		}

		if (UtilValidate.isNotEmpty(userId)) {
			wrapper.and(w -> w.eq("company_id", userId)
							.or().eq("hr_id", userId)
							.or().eq("user_id", userId));
		}

		if (UtilValidate.isNotEmpty(searchValue)) {
			wrapper.and(w -> w.like("user_name", searchValue)
					.or().like("hr_name", searchValue)
					.or().like("hunting_status", searchValue)
					.or().like("job_name", searchValue));
		}
		wrapper.orderByDesc("create_date");
		wrapper.ne("deleted", Constants.getYetDel());
		if (UtilValidate.isEmpty(pageSize)){
			page = new Page(currentPage, Constants.getPageSize());
		}else {
			page = new Page(currentPage, pageSize);
		}

		IPage huntings = huntingStatusMapper.selectPage(page, wrapper);
		result = Result.succeed(huntings);

		return result;
	}

	@Override
	public Result selectOneHunting(HuntingStatus huntingStatus) {
		if (UtilValidate.isEmpty(huntingStatus)){
			return Result.failed("参数为空");
		}

		Result result = profileService.selectProfileByUserId(huntingStatus.getUserId());
		ProfileDto profileDto = (ProfileDto) result.getDatas();
		if (UtilValidate.isEmpty(profileDto)){
			return Result.failed("简历信息查询失败");
		}
		huntingStatus.setProfileDto(profileDto);
		return Result.succeed(huntingStatus);
	}

	@Override
	public int addHunting(HuntingStatus huntingStatus) {
		if (UtilValidate.isEmpty(huntingStatus)){
			return -1;
		}
		return huntingStatusMapper.insert(huntingStatus);
	}

	@Override
	public int updateHuntingStatus(HuntingStatus huntingStatus) {
		if (UtilValidate.isEmpty(huntingStatus)){
			return -1;
		}
		return huntingStatusMapper.updateById(huntingStatus);
	}

	@Override
	public int deleteHunting(String HuntingId, Integer deleted) {
		if (UtilValidate.isEmpty(HuntingId)){
			return -1;
		}
		int row = 0;
		if (Objects.equals(deleted, Constants.getNoDel())){
			HuntingStatus hunting = new HuntingStatus();
			hunting.setHuntingId(HuntingId);
			hunting.setDeleted(Constants.getYetDel());
			row = huntingStatusMapper.updateById(hunting);
		}else {
			row = huntingStatusMapper.deleteById(HuntingId);
		}
		return row;
	}

	@Override
	public Result selectHuntingUsers(Integer currentPage, Integer pageSize, String searchValue, String userId) {
		Result result = null;
		QueryWrapper<HuntingStatus> wrapper = new QueryWrapper<>();
		Page page = null;

		if (UtilValidate.isNotEmpty(searchValue)){
			wrapper.and(w -> w.like("user_name", searchValue)
					.or().like("hunting_status", searchValue));
		}

		if (UtilValidate.isNotEmpty(userId)) {
			wrapper.and(w -> w.eq("company_id", userId)
					.or().eq("hr_id", userId));
		}

		wrapper.ne("hunting_status", "不感兴趣");
		wrapper.ne("deleted", Constants.getYetDel());

		if (UtilValidate.isEmpty(pageSize)){
			page = new Page(currentPage, Constants.getPageSize());
		}else {
			page = new Page(currentPage, pageSize);
		}

		IPage huntings = huntingStatusMapper.selectPage(page, wrapper);

		List<HuntingStatus> records = huntings.getRecords();

		for (HuntingStatus hunting : records) {

			Result baseInfoRes = baseInfoService.selectBaseInfoById(hunting.getUserId());
			BaseInfo baseInfo = (BaseInfo) baseInfoRes.getDatas();
			List<Skill> skills = baseInfoService.selectSkillById(hunting.getUserId());

			ProfileDto profileDto = new ProfileDto();
			profileDto.setBaseInfo(baseInfo);
			profileDto.setSkills(skills);

			hunting.setProfileDto(profileDto);
		}

		result = Result.succeed(huntings);

		return result;
	}

	@Override
	public Result selectHuntingByIds(String userId, String hrId, String companyId, String jobId) {
		QueryWrapper<HuntingStatus> wrapper = new QueryWrapper<>();
		if (UtilValidate.isNotEmpty(userId)) {
			wrapper.eq("user_id", userId);
		}
		if (UtilValidate.isNotEmpty(hrId)) {
			wrapper.eq("hr_id", hrId);
		}
		if (UtilValidate.isNotEmpty(companyId)) {
			wrapper.eq("company_id", companyId);
		}
		if (UtilValidate.isNotEmpty(jobId)) {
			wrapper.eq("job_id", jobId);
		}
		List<HuntingStatus> list = huntingStatusMapper.selectList(wrapper);
		return Result.succeed(list);
	}

	@Override
	public Result countHunting() {
		Integer integer = huntingStatusMapper.selectCount(null);
		if (UtilValidate.isNotEmpty(integer)) {
			return Result.succeed(integer, "查询成功");
		}else {
			return Result.failed("查询失败");
		}
	}
}
